<div class="row">
  <div class="label">#{ifnot _element.isHidden()}${_element.label}#{/ifnot}</div>
  <div class="field">
    #{if _element.isCheckbox() }
      <input type="checkbox" name="entity.${_element.path}"
        #{scaffold.expression entity:_element.path('?.'), expr:'entity?.$1 ? \'checked\':\'\'' /}
      />
    #{/if}
    #{elseif _element.isDate()}
      <script type="text/javascript">
        $(function() {
          $("#${_element.path}").datepicker({ dateFormat: 'yy-mm-dd' } );
        });
      </script>
      <!-- TODO: date format needs to come from application.conf -->
      <input id="${_element.path}" type="text" name="entity.${_element.path}" 
        value="#{scaffold.expression entity:_element.path('?.'), expr:'entity?.$1?.format(\'yyyy-MM-dd\')' /}"
        size="10" />
    #{/elseif}
    #{elseif _element.isSelect()}
      <select name="entity.${_element.path}">
        #{list _element.options, as: 'option'}
          <option value="${option}" 
            #{scaffold.expression entity:[_element.path('?.'),option], expr:'(entity?.$1?.name() == \'$2\') ? \'selected\':\'\'' /}>${option}</option>
        #{/list}
      </select>
    #{/elseif}
    #{elseif _element.isRelation()}
      #{if _element.isMultiple()}
        <select name="entity.${_element.path}.id" multiple>
        ${play.modules.scaffold.utils.Tags.openScriptTag} ${play.modules.scaffold.generator.DatabaseAccessScaffolding.allFor(_element.fieldType)}.sort{ it.toString() }.each() { }%
          %{ String idField = play.modules.scaffold.entity.Entity.from(_element.fieldType).idField %}
          ${play.modules.scaffold.utils.Tags.openScriptTag} 
            selected = false;
            entity?.${_element.path}.each() { f ->
              if (f.id == it?.id)
                selected = true;
            }
          }%
          <option value="#{scaffold.expression entity:idField, expr:'it?.$1'/}" #{scaffold.expression expr:'selected ? \'selected\'.raw() : \'\''/}>#{scaffold.expression entity:'it', expr:'$1'/}</option>
        ${play.modules.scaffold.utils.Tags.openScriptTag} } }%
        </select>
      #{/if}
      #{else}
        <select name="entity.${_element.path}.id">
              #{ifnot _element.isRequired()}
                      <option value="">&{'scaffold.none'}</option>              
              #{/ifnot}
        ${play.modules.scaffold.utils.Tags.openScriptTag} ${play.modules.scaffold.generator.DatabaseAccessScaffolding.allFor(_element.fieldType)}.sort{ it.toString() }.each() { }%
          %{ String idField = play.modules.scaffold.entity.Entity.from(_element.fieldType).idField %}
          ${play.modules.scaffold.utils.Tags.openScriptTag}
            selected = false;
            if (entity?.${_element.path}?.${idField} == it.id) {
              selected = true
            }
          }%
          <option value="#{scaffold.expression entity:idField, expr:'it?.$1'/}" #{scaffold.expression expr:'selected ? \'selected\'.raw() : \'\''/}>#{scaffold.expression entity:'it', expr:'$1'/}</option>
        ${play.modules.scaffold.utils.Tags.openScriptTag} } }%
        </select>
      #{/else}
    #{/elseif}
    #{elseif _element.isList()}
    	#{if Enum.class.isAssignableFrom(_element.getFieldType())}
			${play.modules.scaffold.utils.Tags.openTemplateTag}list items:${_element.getFieldType().getName()}.values(), as:'en'} <input
				type="checkbox" name="entity.${_element.path}" value="#{scaffold.expression entity:'en', expr:'$1'/}" ${play.modules.scaffold.utils.Tags.openTemplateTag}if entity?.${_element.path}?.contains(en)} checked ${play.modules.scaffold.utils.Tags.openTemplateTag}/if}>#{scaffold.expression entity:'en', expr:'$1'/}</input>
				<br />
			${play.modules.scaffold.utils.Tags.openTemplateTag}/list}    	
    	#{/if}
    #{/elseif}
    #{elseif _element.isTextArea()}
      <textarea name="entity.${_element.path}" rows="10" cols="40">#{scaffold.expression entity:_element.path('?.'), expr:'entity?.$1' /}</textarea>
    #{/elseif}
    #{elseif _element.isPassword()}
      <input type="password" name="entity.${_element.path}" value="#{scaffold.expression entity:_element.path('?.'), expr:'entity?.$1' /}"/>
    #{/elseif}
    #{elseif _element.isHidden()}
      <input type="hidden" name="entity.${_element.path}" value="#{scaffold.expression entity:_element.path('?.'), expr:'entity?.$1' /}"/>
    #{/elseif}
    #{elseif _element.isEmbedded()}
        #{list items:_element.fieldAsEntity.formElements, as:'elem'}
         #{scaffold.element element:elem /}
        #{/list}        
    #{/elseif}
    #{else}
      <input type="text" name="entity.${_element.path}" value="#{scaffold.expression entity:_element.path('?.'), expr:'entity?.$1' /}"/>
    #{/else}
  </div>
  <span class="error">#{scaffold.expression entity:_element.path, expr:'errors.forKey(\'entity.$1\')' /}</span>
</div>
